package m.base.util;

import com.alibaba.fastjson.JSONObject;
import m.system.exception.MException;
import m.system.util.ArrayUtil;
import m.system.util.HttpRequestUtil;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ZolUtil {
    public static void main(String[] a) throws Exception {
        getGoodsUrls("https://detail.zol.com.cn/notebook_index/subcate16_list_1.html");
    }
    public static String[] getGoodsUrls(String text) throws Exception {
        Set<String> urls=new HashSet<>();
        String html="";
        if(text.startsWith("http")){
            HttpRequestUtil util=new HttpRequestUtil();
            util.setCharset("GBK");
            html=util.doGet(text);
        }else{
            html=text;
        }
        Pattern p=Pattern.compile("/index\\d+?\\.");
        Matcher m=p.matcher(html);
        while(m.find()){
            String str=m.group();
            str=str.substring(6,str.length()-1);
            int id=Integer.parseInt(str);
            int tmp=new Double(Math.ceil(id /1000.0)).intValue();
            String url="https://detail.zol.com.cn/"+tmp+"/"+id+"/param.shtml";
            urls.add(url);
        }
        return urls.toArray(new String[0]);
    }
    public static Integer getOutId(String url) throws MException {
        if(url.contains("?")) url=url.substring(0,url.indexOf("?"));
        if(Pattern.compile("/[0-9]*/param.shtml$").matcher(url).find()){
            String tmp=url.substring(url.lastIndexOf("/")-7,url.lastIndexOf("/"));
            if(tmp.contains("/")) tmp=tmp.substring(tmp.indexOf("/")+1);
            return Integer.parseInt(tmp);
        }else if(Pattern.compile("/index[0-9]*.shtml$").matcher(url).find()){
            String tmp=url.substring(url.lastIndexOf("/")+6,url.length()-6);
            return Integer.parseInt(tmp);
        }else{
            throw new MException(ZolUtil.class,"地址错误");
        }
    }
    public static GoodsParam convertGoodsParam(Integer id) throws Exception {
        if(null==id) throw new MException(ZolUtil.class,"外部链接不存在");
        int tmp=new Double(Math.ceil(new Double(id)/1000.0)).intValue();
        String url="https://detail.zol.com.cn/"+tmp+"/"+id+"/param.shtml";
        GoodsParam param=new GoodsParam();
        HttpRequestUtil util=new HttpRequestUtil();
        util.setCharset("GBK");
        String txt=util.doGet(url);
        Pattern p=Pattern.compile("zolproduct = \".+?\";");
        Matcher m=p.matcher(txt);
        if(m.find()){
            String str=m.group();
            str=str.substring(14,str.length()-2);
            param.setGoodsName(str);//商品名称
        }
        p=Pattern.compile("zolSubName = '.+?';");
        m=p.matcher(txt);
        if(m.find()){
            String str=m.group();
            str=str.substring(14,str.length()-2);
            param.setTypeName(str);//类型名称
        }
        p=Pattern.compile("zolManuCnName = '.+?';");
        m=p.matcher(txt);
        if(m.find()){
            String str=m.group();
            str=str.substring(17,str.length()-2);
            param.setFactoryName(str);//厂商名称
        }
        Pattern p1=Pattern.compile("class=\"hd\".+?</table>");
        Pattern p2=Pattern.compile("colspan=\"2\".*?>.+?<");
        Matcher m1=p1.matcher(txt.replaceAll("\r\n",""));
        while(m1.find()){
            String str1=m1.group();
            Matcher m2=p2.matcher(str1.replaceAll("<br/>",""));
            while(m2.find()) {
                String str2=m2.group();
                String groupName=str2.substring(str2.lastIndexOf(">")+1,str2.length()-1).trim();
                if(StringUtil.isSpace(groupName)) continue;
                param.getAttrList().add(fillAttrs(str1,groupName));
            }
        }
        return param;
    }
    private static JSONMessage fillAttrs(String txt, String groupName){
        List<JSONMessage> attrs=new ArrayList<>();
        Pattern p1=Pattern.compile("<th.+?</td>");
        Pattern p2=Pattern.compile(">([^<]+?)<");
        Matcher m1=p1.matcher(txt.replaceAll("\r\n",""));
        while(m1.find()){
            String str1=m1.group();
            Matcher m2=p2.matcher(str1.replaceAll("<br />","\n"));
            boolean b=true;
            List<String> vs=new ArrayList<>();
            JSONMessage attr=new JSONMessage();
            while(m2.find()){
                String str2=m2.group().replaceAll("&nbsp;"," ");
                String s=str2.substring(1,str2.length()-1).trim();
                if(!s.isEmpty() &&s.lastIndexOf(">")==s.length()-1) s=s.substring(0,s.length()-1);
                if(StringUtil.notSpace(s)&&!"纠错".equals(s)&&!s.startsWith("更多")&&!s.startsWith("进入官网")&&!s.startsWith("击败")){
                    if(b){
                        attr.push("name",s);
                        b=false;
                    }else{
                        vs.add(s);
                    }
                }
            }
            attr.push("value", ArrayUtil.connection(vs.toArray(new String[0]),""));
            attrs.add(attr);
        }
        return new JSONMessage().push("groupName",groupName).push("attrs",attrs);
    }
}
